//
//  SEPrescriptionOrderDetailViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/17.
//

import Foundation
import UIKit

class SEPrescriptionOrderDetailViewModel: SEBaseViewModel {

    var model = SEPrescriptionOrderDetailModel()

    convenience init(model: SEPrescriptionOrderDetailModel) {
        self.init()
        self.model = model
    }

    var numberOfSections: Int {
        4
    }

    func numberOfRowsInSection(section: Int) -> Int {
        switch section {
        case 0: return 1
        case 1: return 2
        case 2: return model.list.count
        case 3: return 1
        case 4: return logisticsModelArray.count
        default: return 0
        }
    }

    var payHandler: ((SEHttpStatus) -> Void)?

    var submitAddressHandler: ((SEHttpStatus) -> Void)?

    var isAgree = false

    var administrative: String {
        if model.orderLogisticsDto.provideName.isEmpty || model.orderLogisticsDto.cityName.isEmpty || model.orderLogisticsDto.areaName.isEmpty {
            return ""
        } else {
            return "\( model.orderLogisticsDto.provideName),\(model.orderLogisticsDto.cityName),\(model.orderLogisticsDto.areaName)"
        }
    }

    var payViewModel: SEConfirmReservePayViewModel {
        return SEConfirmReservePayViewModel(price: String(format: "%.2lf", model.totalExpense / 100.0), time: "", agree: isAgree)
    }

    var patientNameDescribe: String {
        return "就诊人: \(model.patientName)"
    }

    var patientSex: SEPatientSex {
        return SEPatientSex(rawValue: model.patientSex) ?? .unknown
    }

    var mainDiagnose: String {
        let models = model.detailDtos.filter {
            $0.isMain == 1
        }
        guard models.count == 1 else { return "" }
        let model = models[0]
        return model.prefix + " - " + model.middle + " - " + model.suffix
    }

    var patientSexDescribe: String {
        return "性别: \(patientSex.title)"
    }

    var patientAgeDescribe: String {
        return "年龄: \(model.patientAge)岁"
    }

    var infoModelArray: [SEHospitalizeOrderInfoCellModel] {
        return [
            SEHospitalizeOrderInfoCellModel(titleText: "诊断：",
                                            memoText: mainDiagnose,
                                            titleColor: nil,
                                            titleFont: nil,
                                            memoColor: nil,
                                            memoFont: nil),
            SEHospitalizeOrderInfoCellModel(titleText: "医嘱：",
                                            memoText: model.proposal,
                                            titleColor: nil,
                                            titleFont: nil,
                                            memoColor: nil,
                                            memoFont: nil)
        ]
    }

    var logisticsModelArray: [SEPatientTitleTextFieldCellModel] {
        return [
            SEPatientTitleTextFieldCellModel(title: "收货人姓名",
                                             placeHolder: "请输入姓名",
                                             cellType: .normal,
                                             text: model.orderLogisticsDto.receiveName,
                                             editEnable: model.orderStatus == .waitpay),
            SEPatientTitleTextFieldCellModel(title: "联系方式",
                                             placeHolder: "请输入联系方式",
                                             cellType: .normal,
                                             text: model.orderLogisticsDto.receiveMobile,
                                             editEnable: model.orderStatus == .waitpay,
                                             keyboardType: .phonePad),
            SEPatientTitleTextFieldCellModel(title: "行政区域",
                                             placeHolder: "请选择您所在区域",
                                             cellType: .tap,
                                             text: administrative,
                                             editEnable: model.orderStatus == .waitpay),
            SEPatientTitleTextFieldCellModel(title: "详细地址",
                                             placeHolder: "请输入详细地址",
                                             cellType: .normal,
                                             text: model.orderLogisticsDto.receiverAddress,
                                             editEnable: model.orderStatus == .waitpay),
        ]
    }

    private var checkPay: String? {
        if model.orderLogisticsDto.receiveName.isEmpty {
            return "请输入收货人姓名"
        }
        if model.orderLogisticsDto.receiveMobile.isEmpty {
            return "请输入联系方式"
        }
        if administrative.isEmpty {
            return "请选择行政区域"
        }
        if model.orderLogisticsDto.receiverAddress.isEmpty {
            return "请输入详细地址"
        }
        if !isAgree {
            return "请阅读并同意患者须知"
        }
        return nil
    }

    /// 处方订单详情
    func requestPrescriptionDetail() {
        SEHttpManager.shared.request(SEProfileAPI.prescriptionOrderDetail(id: model.id), model: SEPrescriptionOrderDetailModel.self, withLoading: true) { model, status in
            switch status {
            case .success:
                self.model = model ?? SEPrescriptionOrderDetailModel()
            default: break
            }
            self.updateData?(status)
        }
    }

    /// 处方订单支付
    func requestPayPrescriptionOrder() {
        SEHttpManager.shared.requestForAny(SEPayAPI.pay(id: model.orderId, payType: 1, payChannel: 1), withLoading: true) { result, status in
            self.payHandler?(status)
        }
    }

    /// 提交处方药品收货信息
    func requestSubmitPrescriptionOrderAddress() {
        if let msg = checkPay {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.requestForAny(SEProfileAPI.prescriptionOrderSubmitAddress(
                                            id: model.orderLogisticsDto.id,
                                            preId: model.id,
                                            name: model.orderLogisticsDto.receiveName,
                                            phone: model.orderLogisticsDto.receiveMobile,
                                            provinceId: model.orderLogisticsDto.provideId,
                                            provinceName: model.orderLogisticsDto.provideName,
                                            cityId: model.orderLogisticsDto.cityId,
                                            cityName: model.orderLogisticsDto.cityName,
                                            areaId: model.orderLogisticsDto.areaId,
                                            areaName: model.orderLogisticsDto.areaName,
                                            address: model.orderLogisticsDto.receiverAddress), withLoading: true) { result, status in
            self.submitAddressHandler?(status)
        }
    }
}
